package cc.yugu.yhdw.webserver.web.controller.security.basic;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.domain.basic.Dept;
import cc.yugu.yhdw.common.domain.basic.User;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.service.basic.DeptService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.Set;
/**
 * 部门控制器
 *
 * @Date:Created in 2018/7/23 11:42
 * @Author:
 * @Modified By: zjn 2018/7/23 11:42
 */
@Controller
@RequestMapping(value = "/security/basic/dept")
public class DeptController extends SecurityController {

    @Autowired
    DeptService deptService;
    /**
    * 返回json格式构建的部门目录树
    *
    * @Param: [dummy, agentId, response]
    * @Return: void
    * @Author By: zjn 2018/7/23  11:43
    */
    @ResponseBody
    @ViewModel(ViewModel.JSON_ARRAY)
    @RequestMapping(value = "tree.htm")
    public void tree(String dummy, Integer agentId, HttpServletResponse response) throws Exception {
        response.setContentType(ConstEnum.ContentType.JSON.getValue());
        Set<Integer> checkedSet = Collections.emptySet();
        deptService.tree(checkedSet, dummy, agentId, response.getOutputStream());
    }

    @RequestMapping(value = "index.htm")
    public void index() {
    }
    /**
    * MENU_CODE_NAME :部门管理
    *
    * @Param: [model]
    * @Return: void
    * @Author By: zjn 2018/7/23  11:45
    */
    @RequestMapping(value = "dept_index.htm")
    public void dept_index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_01_08.getValue());
    }
    /**
    * 分页查询
    *
    * @Param: [search, request]
    * @Return: cc.yugu.yhdw.common.entity.json.PageResult
    * @Author By: zjn 2018/7/23  11:46
    */
    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(Dept search, HttpServletRequest request) {
        // 如果是运营商或者代理商登录
        if (getSessionUser(request).getType() == User.Type.AGENT.getValue()||getSessionUser(request).getType() == User.Type.AGENCY.getValue()) {
            search.setAgentId(getSessionUser(request).getAgentId());
        }
        return PageResult.successResult(deptService.findPage(search));
    }

    @RequestMapping(value = "add.htm")
    public void add() {
    }
    /**
    * 返回到部门管理的修改页面
    *
    * @Param: [model, id]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/23  11:49
    */
    @RequestMapping(value = "edit.htm")
    public String edit(Model model, int id) {
        // 查询部门信息
        Dept entity = deptService.find(id);
        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        return "/security/basic/dept/edit";
    }
    /**
    * 新建部门，插入数据库
    * 
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/23  11:57
    */ 
    @RequestMapping("create.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult create(Dept entity) {
        return  deptService.insert(entity);
    }
    /**
    * 更新部门表
    * 
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/23  11:57
    */ 
    @RequestMapping("update.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult update(Dept entity) {
        return deptService.update(entity);
    }
    /**
    * 删除部门信息
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/23  11:57
    */ 
    @RequestMapping("delete.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult delete(int id) {
        return deptService.delete(id);
    }
    /**
    * 点击查看，部门信息
    * 
    * @Param: [model, id]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/23  11:57
    */ 
    @RequestMapping(value = "view.htm")
    public String view(Model model, int id) {
        Dept entity = deptService.find(id);
        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        return "/security/basic/dept/view";
    }
}
